/**
 * Licensed to Apereo under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright ownership. Apereo
 * licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the License at the
 * following location:
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 *
 * <p>Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apereo.portal.rendering.predicates;

import java.util.function.Predicate;
import javax.servlet.http.HttpServletRequest;
import org.apereo.portal.url.IPortalRequestInfo;
import org.apereo.portal.url.IUrlSyntaxProvider;
import org.apereo.portal.url.UrlState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Answers whether a given HttpServletRequest represents one that the rendering pipeline will focus
 * on rendering just one portlet (e.g., maximized, or exclusive).
 *
 * @since 4.2
 */
public class URLInSpecificStatePredicate implements Predicate<HttpServletRequest> {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    // auto-wired.
    private IUrlSyntaxProvider urlSyntaxProvider;

    private String state;
    private boolean isNegated;

    @Override
    public boolean test(final HttpServletRequest request) {

        final IPortalRequestInfo portalRequestInfo =
                this.urlSyntaxProvider.getPortalRequestInfo(request);

        if (null == portalRequestInfo) {
            logger.warn(
                    "Portal request info was not available for this request, "
                            + "so assuming that it does not represent focus on one portlet.");
            // False when portalRequestInfo is null because unknown portal state is not
            // focused-on-one-portlet portal state.
            return false;
        }

        final UrlState urlState = portalRequestInfo.getUrlState();
        boolean stateEqual = urlState.toString().equals(state);

        return isNegated ? !stateEqual : stateEqual;
    }

    @Autowired
    public void setUrlSyntaxProvider(IUrlSyntaxProvider urlSyntaxProvider) {
        this.urlSyntaxProvider = urlSyntaxProvider;
    }

    public void setNegated(boolean isNegated) {
        this.isNegated = isNegated;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}
